<?php

/**
 * This file is part of Switcher.
 * 
 * Switcher is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Switcher is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Switcher. If not, see <http://www.gnu.org/licenses/>.
 * 
 * @license http://www.opensource.org/licenses/gpl-3.0.html GPL-3.0
 */

namespace switcher\utils;

final class FileHelperTest extends \PHPUnit_Framework_TestCase
{
	private static $FNM_FLAGS = array(FNM_NOESCAPE, FNM_PATHNAME, FNM_PERIOD, FNM_CASEFOLD, FNM_LEADING_DIR);
	
	public function testShouldIndentString()
	{
		$string = "a\nb\nc\nd";
		$expected = "\ta\n\tb\n\tc\n\td";
		$this->assertEquals($expected, FileHelper::indentString($string, "\t"), "Indent a string");
	}
	
	public function testShouldNotIndentTheLastBlankLine()
	{
		$string = "a\nb\nc\nd\n";
		$expected = "\ta\n\tb\n\tc\n\td\n";
		$this->assertEquals($expected, FileHelper::indentString($string, "\t"), "Indent a string except the last blank line");
	}
	
	/** @dataProvider fnmatchTests */
	public function testMatchShouldBehaveLikeFnmatch($string, $pattern)
	{
		foreach (self::$FNM_FLAGS as $flag)
		{
			$match = FileHelper::match($pattern, $string, $flag) !== false;
			$this->assertEquals(fnmatch($pattern, $string, $flag), $match, "$pattern : $string");
		}
	}
	
	public function fnmatchTests()
	{
		return array(
			array('\\', '[\\]'),
			array('a', '[!\\]'),
			array('\\', '[!\\]'),
			array('doc_draft_', 'doc[_[]draft[_\\]]'),
			array('doc[draft]', 'doc[_[]draft[_\\]]'),
			array('foo\nbar', 'foo*'),
			array('foo\nbar\n', 'foo*'),
			array('\nfoo', 'foo*'),
			array('\n', '*'),
			array('.wrong', '?wrong'),
			array('.right', '.right'),
			array('trailing//', 'trailing')
		);
	}
}

?>